package cn.wolfcode.cloud.shop.ws;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;


@ServerEndpoint("/{uuid}")
@Component
public class WebSocketServer {
//    private Session session;
    public static ConcurrentHashMap<String,Session> clients = new ConcurrentHashMap<>();

    private Logger logger = LoggerFactory.getLogger("WEB-SOCKET-SERVER-LOGGER");
    @OnOpen
    public void onOpen(Session session, @PathParam( "uuid") String uuid){
        logger.info("客户端:"+uuid+"已连接");
//        this.session = session;
        clients.put(uuid,session);
    }
    @OnClose
    public void onClose(@PathParam( "uuid") String uuid){
        logger.info("客户端:"+uuid+"已断开");
        clients.remove(uuid);
    }
    @OnError
    public void onError(Throwable error) {
        error.printStackTrace();
    }
}
